package com.sy.simpleegg.util

import android.annotation.SuppressLint
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale

object DateTimeUtil {
    val DATE_PATTERN_DATE = "yyyy-MM-dd"
    val DATE_PATTERN_TIME = "HH:mm:ss"
    val DATE_PATTERN_DATE_TIME = "yyyy-MM-dd HH:mm:ss"
    val DATE_PATTERN_DATE_TIME_SP = "yyyyMMddHHmmss"
    val DATE_PATTERN_DATE_2 = "yyyyMMdd"
    val DATE_PATTERN_TIME_2 = "HHmmss"
    val DATE_PATTERN_TIME_HHMM = "HH:mm"

    val now: Date
        get() = Date(Date().time)

    val nowDate: String
        get() = formatDate(now, DATE_PATTERN_DATE)

    val nowTime: String
        get() = formatDate(now, DATE_PATTERN_TIME)

    val nowDateTime: String
        get() = formatDate(now, DATE_PATTERN_DATE_TIME)

    val nowDateTimeSp: String
        get() = formatDate(now, DATE_PATTERN_DATE_TIME_SP)

    val nowDateTimeBeforeMonth: String
        get() = formatDate(now, DATE_PATTERN_DATE_TIME_SP)

    val nowDate2: String
        get() = formatDate(now, DATE_PATTERN_DATE_2)

    val nowDateTime2: String
        get() = formatDate(now, DATE_PATTERN_TIME_2)

    val timeStampS : String
        get() = timeStapSecond()

    val dateHHmm: String
        get() = formatDate(now, DATE_PATTERN_TIME_HHMM)

    @SuppressLint("SimpleDateFormat")
    fun formatDate(date: Date?, formatStyle: String): String{
        return if (date == null){
            ""
        }else{
            val sdf = SimpleDateFormat(formatStyle)
            sdf.format(date)
        }
    }

    fun formatDate(formatStyle: String, formatStr: String): Date{
        val format = SimpleDateFormat(formatStyle, Locale.CHINA)
        return try {
            val date = Date()
            date.time = format.parse(formatStr).time
            date
        }catch (e: Exception) {
            println(e.message)
            now
        }
    }

    @SuppressLint("SimpleDateFormat")
    fun dateStrToLong(dateStr: String): Long{
        val format = SimpleDateFormat(DATE_PATTERN_DATE_TIME)
        return format.parse(dateStr)!!.time
    }

    fun getDaysAgoDate(lastDays: Int): String{
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.DAY_OF_MONTH, -lastDays)
        val date = calendar.time
        return formatDate(date, DATE_PATTERN_DATE)
    }

    fun getDatesPass(days: Int): String{
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.DAY_OF_MONTH, -days)
        val date = calendar.time
        return formatDate(date, DATE_PATTERN_DATE_2)
    }

    fun timeStapSecond(): String{
        val ts = System.currentTimeMillis() / 1000
        return ts.toString()
    }
}